package com.hzt.agprosaleplatform.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.hzt.agprosaleplatform.entity.Order;
import com.hzt.agprosaleplatform.entity.OrderDetailDTO;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Many;

import java.util.List;
import java.util.Map;

public interface OrderMapper extends BaseMapper<Order> {

    //通过购买者ID查询所有订单   --用户
    @Select("SELECT * FROM t_orders WHERE buyer_id = #{uid} ")
    List<Order> selectOrderByUid(String uid);

    //通过商铺ID查询所有订单   --商家
    @Select("SELECT * FROM t_orders WHERE shop_id = #{shopid} ")
    List<Order> selectOrderByShopId(@Param("shopid") Integer shopId);



    @Update("UPDATE t_orders SET status = #{status} WHERE oid = #{oid}")
    boolean updateOrderStatus(@Param("oid") Integer oid, @Param("status") Integer status);

    @Select("SELECT * FROM t_orders WHERE oid = #{oid}")
    Order selectOrderByOid(Integer oid);

    @Select("SELECT " +
            "DISTINCT o.oid, o.shop_id, o.address_id, o.order_message, o.status, o.total_amount, " +
            "o.created_date, o.update_date " +
            "FROM t_orders o " +
            "WHERE o.buyer_id = #{uid}")
    @Results({
            @Result(property = "oid", column = "oid"),
            @Result(property = "shop_id", column = "shop_id"),
            @Result(property = "address_id", column = "address_id"),
            @Result(property = "order_message", column = "order_message"),
            @Result(property = "status", column = "status"),
            @Result(property = "total_price", column = "total_amount"),
            @Result(property = "created_date", column = "created_date"),
            @Result(property = "update_date", column = "update_date"),
            @Result(property = "goods", column = "oid",
                    many = @Many(select = "selectGoodsByOrderId"))
    })
    List<OrderDetailDTO> selectOrderDetailsByUid(String uid);

    @Select("SELECT " +
            "g.gid, g.img as goodsImg, g.title, g.tags as tagsStr, " +
            "op.unit_price, op.amount, op.sub_total as sub_price " +
            "FROM order_products op " +
            "JOIN t_goods g ON op.gid = g.gid " +
            "WHERE op.oid = #{oid}")
    List<OrderDetailDTO.GoodsItemDTO> selectGoodsByOrderId(Integer oid);
    
    // 统计店铺订单数
    @Select("SELECT COUNT(*) FROM t_orders WHERE shop_id = #{shopId}")
    int countOrdersByShopId(@Param("shopId") Integer shopId);
    
    // 统计店铺总收入
    @Select("SELECT SUM(total_amount) FROM t_orders WHERE shop_id = #{shopId} AND status = 4")
    Double sumOrderAmountsByShopId(@Param("shopId") Integer shopId);
    
    // 统计店铺待处理订单数
    @Select("SELECT COUNT(*) FROM t_orders WHERE shop_id = #{shopId} AND (status = 0 OR status = 2)")
    int countPendingOrdersByShopId(@Param("shopId") Integer shopId);
    
    // 获取店铺收入趋势
    @Select("SELECT DATE_FORMAT(created_date, '%Y-%m') as month, SUM(total_amount) as revenue " +
            "FROM t_orders " +
            "WHERE shop_id = #{shopId} AND status = 4 " +
            "GROUP BY DATE_FORMAT(created_date, '%Y-%m') " +
            "ORDER BY month")
    List<Map<String, Object>> selectRevenueTrend(@Param("shopId") Integer shopId);
    
    // 获取订单状态统计
    @Select("SELECT " +
            "CASE status " +
            "  WHEN 0 THEN '待付款' " +
            "  WHEN 1 THEN '已取消' " +
            "  WHEN 2 THEN '已付款' " +
            "  WHEN 3 THEN '已发货' " +
            "  WHEN 4 THEN '已完成' " +
            "  WHEN 5 THEN '已退款' " +
            "END as name, " +
            "COUNT(*) as value " +
            "FROM t_orders " +
            "WHERE shop_id = #{shopId} " +
            "GROUP BY status")
    List<Map<String, Object>> selectOrderStatusStats(@Param("shopId") Integer shopId);
}